package com.chaopei.msite_web.controller;

import com.chaopei.msite_web.entity.UserInfo;
import com.chaopei.msite_web.service.UserSevice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @Author Corday 天涯
 * @Date 2022/4/29 15:57
 */
@Controller
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Resource
    private UserSevice userSevice;

    /**
     * 到登录页
     * @return
     */
    @RequestMapping("/goLogin")
    public String goLogin(){
        return "pages/login";
    }

    /**
     * 到注册页
     * @return
     */
    @RequestMapping("/goReg")
    public String goReg(){
        return "pages/register";
    }
    /**
     * 执行登录,成功后回首页
     * @param userName
     * @param password
     * @param session
     * @return
     */
    @RequestMapping("/doLogin")
    public String login(String userName, String password, HttpSession session) {
        String result = userSevice.userLogin(userName, password);
        //log.info("dologin拿到:"+result);
        if("error".equals(result)){
            session.setAttribute("msg","网络阻塞,稍候请刷新...");
            return "pages/error";
        }else{
            if(result==null){
                session.setAttribute("msg", "用户名或密码有误");
                return "pages/login";
            }else{//用户名与密码正确
                session.setAttribute("uid", result);
                session.setAttribute("userName", userName);
                return "pages/index";
            }
        }
    }

    /**
     * 执行注册
     * @param userInfo
     * @param map
     * @return
     */
    @RequestMapping("/doReg")
    public String register(UserInfo userInfo, Map map,HttpSession session) {
        String info = userSevice.userRegister(userInfo);
        if("no".equals(info)){
            session.setAttribute("msg","网络阻塞,稍候请刷新...");
            return "pages/error";
        }else{
            map.put("info",info);
            return "pages/register";
        }
    }


}
